﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Collections.Specialized;

public partial class Pledges : Acms.AcmsControls.ContentControls.Admin.BaseCustomAdminControl
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        if (Request.QueryString["Id"] != null)
        {
            try
            {
                CatId = int.Parse(Request.QueryString["Id"]);
            }
            catch (FormatException) { }
        }
        dsPledges.Select += new EventHandler<Acms.DataControls.EventDrivenDataSourceSelectEventArgs>(dsPledges_Select);
        dsPledges.Insert += new EventHandler<Acms.DataControls.EventDrivenDataSourcePerformOperationEventArgs>(dsPledges_Save);
        dsPledges.Update += new EventHandler<Acms.DataControls.EventDrivenDataSourcePerformOperationEventArgs>(dsPledges_Save);
        dsPledges.Delete += new EventHandler<Acms.DataControls.EventDrivenDataSourcePerformOperationEventArgs>(dsPledges_Save);
        dsTypes.Select += new EventHandler<Acms.DataControls.EventDrivenDataSourceSelectEventArgs>(dsTypes_Select);
        grid.ItemCommand += new Telerik.Web.UI.GridCommandEventHandler(grid_ItemCommand);
    }

    void dsPledges_Delete(object sender, Acms.DataControls.EventDrivenDataSourcePerformOperationEventArgs e)
    {
        throw new NotImplementedException();
    }

    void grid_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "MoveUp" || e.CommandName == "MoveDown")
        {
            var factory = new CarbonExchange.Bll.PledgeFactory();
            var items = factory.GetAllByCategoryId(CatId).ToList();
            int step = e.CommandName == "MoveUp" ? -1 : 1;
            int index = e.Item.DataSetIndex;
            factory.Move(items, index, index + step);
            grid.SelectedIndexes.Clear();
            grid.Rebind();
            grid.MasterTableView.Items[e.Item.ItemIndex + step].Selected = true;
        }
        else if (e.CommandName == RadGrid.InitInsertCommandName && grid.Items.Count == 0)
        {

            //Add new" button clicked
            e.Canceled = true;
            //Prepare an IDictionary with the predefined values
            ListDictionary newValues = new ListDictionary();
            newValues["AveSavingPerMonth"] = "";
            //Insert the item
            e.Item.OwnerTableView.InsertItem(newValues);
        }
    }

    void dsTypes_Select(object sender, Acms.DataControls.EventDrivenDataSourceSelectEventArgs e)
    {
        var tmp = Enum.GetNames(typeof(CarbonExchange.Bll.Pledge.SolutionTypes)).Where(x => x != CarbonExchange.Bll.Pledge.SolutionTypes.Unknown.ToString()).Select(x => new {Name = x});
        e.SetData(tmp);
    }

    void dsPledges_Save(object sender, Acms.DataControls.EventDrivenDataSourcePerformOperationEventArgs e)
    {
        var factory = new CarbonExchange.Bll.PledgeFactory();
        CarbonExchange.Bll.Pledge tmp = null;
        if (e.Operation == Acms.DataControls.EventDrivenDataSourcePerformOperationEventArgs.Operations.Insert)
        {
            tmp = new CarbonExchange.Bll.Pledge();
            tmp.PledgeCategoryId = CatId;
        }
        else
        {
            int id = (int)e.Keys["Id"];
            tmp = factory.GetById(id);
            tmp.EnsureConcurent(e.Keys["CID"]);
        }

        if (e.Operation == Acms.DataControls.EventDrivenDataSourcePerformOperationEventArgs.Operations.Delete)
        {
            factory.Delete(tmp);
        }
        else
        {
            tmp.Name = (string)e.NewValues["Name"];
            tmp.Recommendation = (string)e.NewValues["Recommendation"];
            tmp.SolutionType = (CarbonExchange.Bll.Pledge.SolutionTypes)Enum.Parse(typeof(CarbonExchange.Bll.Pledge.SolutionTypes), (string)e.NewValues["SolutionType"]);
            tmp.Text = (string)e.NewValues["Text"];
            try
            {
                tmp.AveSavingPerMonth = decimal.Parse((string)e.NewValues["AveSavingPerMonth"]);
            }
            catch (FormatException) { }
            factory.Save(tmp);
        }
    }

    void dsPledges_Select(object sender, Acms.DataControls.EventDrivenDataSourceSelectEventArgs e)
    {
        var factory = new CarbonExchange.Bll.PledgeFactory();
        e.SetData(factory.GetAllByCategoryId(CatId));
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
        }
    }

    private int CatId { get; set; }
    protected override object SaveViewState()
    {
        ViewState["CatId"] = CatId;
        return base.SaveViewState();
    }

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        CatId = (int)ViewState["CatId"];
    }

    public override void GetExtaBreadCrumbs(Dictionary<string, string> extaItems)
    {
        base.GetExtaBreadCrumbs(extaItems);
        if (CatId > 0)
        {
            var factory = new CarbonExchange.Bll.PledgeCategoryFactory();
            var cat = factory.GetById(CatId);
            if (cat != null)
            {
                extaItems.Add(cat.SectionName, ResolveCustomControlPath(string.Format("~/PledgeCategories.ascx?Id={0}", cat.Id)));
                extaItems.Add(cat.Name, ResolveCustomControlPath(string.Format("~/PledgeCategories.ascx?Id={0}", cat.Id)));
            }
        }
    }
}
